package com.luckdraw.service.game;

import java.util.Random;
import java.util.Vector;

import org.nutz.json.Json;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * 活动类
 * @ClassName: Game 
 * @author gaotaochen
 * @Description: 
 * @date 2017年1月18日 下午8:17:07 
 *
 */
public class Game {

	private static final Log log = Logs.getLog("game");
	
	/**
	 * 奖池大小
	 */
	private int rewardSize = 100;

	/**
	 * 盈利人数
	 */
	private int profit = 15;

	/**
	 * 参与成本（单位分）
	 */
	private int cost = 100;

	/**
	 * 参与比例(扣除佣金后所剩成本比例)
	 */
	private double mult = 0.6;

	/**
	 * 最低获得比例
	 */
	private double lowestMult = 0.1;

	/**
	 * 最低获得
	 */
	private int lowest = (int) (cost * lowestMult);

	/**
	 * 最高获得比例
	 */
	private double mostMult = 1;

	/**
	 * 最高获得
	 */
	private int most = (int) (cost * mostMult);

	private Vector<Integer> rewardList = new Vector<Integer>();

	/**
	 * 初始化奖池
	 */
	public void initRewardList() {
		lowest = (int) (cost * lowestMult);
		most = (int) (cost * mostMult);
		rewardList.clear();
		/**
		 * 计算总奖池
		 */
		int totalAmount = (int) (cost * rewardSize * mult) - ((profit * cost) + ((rewardSize - profit) * lowest));
		/**
		 * 初始化默认奖池
		 */
		for (int i = 0; i < rewardSize; i++) {
			int amount = 0;
			Random r = new Random();
			// 生成0.99-0.1之间的随机数
			double random = ((double) (r.nextInt(99) % (99 - 10 + 1) + 10)) / 100;
			if (i < profit) {// 计算盈利玩家所获得的奖励
				amount = (int) ((totalAmount > most ? most : totalAmount) * random);
				if (amount > most)
					amount = most;
				rewardList.add(amount + cost);
				totalAmount = totalAmount - amount;
			} else if (i == rewardSize - 1) {// 最后一人为剩余奖池金额加最低获得金额
				rewardList.add(totalAmount + lowest);
			} else {// 其他玩家的奖励
				amount = (int) ((totalAmount > most ? most : totalAmount) * random);
				// 若奖励大于1时对奖励金额进行修正
				if ((amount + lowest) > cost) {
					random = random - 0.1;
					amount = (int) ((totalAmount > most ? most : totalAmount) * random);
				}
				rewardList.add(amount + lowest);
				totalAmount = totalAmount - amount;
			}
		}
		rewardList = randomList(rewardList);
//		log.info("gameRewardList.init|"+this+"|rewardSize:"+this.rewardSize+";cost:"+cost+";profit:"+profit+";rewardList:"+Json.toJson(rewardList));
//		System.out.println(Json.toJson(rewardList));
	}

	/**
	 * 获取本次奖励
	 * 
	 * @return
	 */
	public int getReward() {
		synchronized (this) {
			int reward = 0;
			if (rewardList.size() > 0) {
				reward = rewardList.get(0);
				rewardList.remove(0);
				return reward;
			} else {
				return 0;
			}
		}
	}

	private boolean isEmpty(Vector<Integer> sourceList) {
		return (sourceList == null || sourceList.size() == 0);
	}

	/**
	 * 打乱list顺序
	 * 
	 * @param sourceList
	 * @return
	 */
	private Vector<Integer> randomList(Vector<Integer> sourceList) {
		if (isEmpty(sourceList)) {
			return sourceList;
		}
		Vector<Integer> randomList = new Vector<Integer>(sourceList.size());
		do {
			int randomIndex = Math.abs(new Random().nextInt(sourceList.size()));
			randomList.add(sourceList.remove(randomIndex));
		} while (sourceList.size() > 0);
		return randomList;
	}
	

	/**
	 * 设置奖池参数
	 * 
	 * @param rewardSize
	 *            奖池大小
	 * @param profit
	 *            盈利人数
	 * @param cost
	 *            参与成本(单位分)
	 * @param mult
	 *            参与比例(扣除佣金后所剩成本比例)
	 * @param lowestMult
	 *            最低获得比例
	 * @param mostMult
	 *            最高奖励比例
	 */
	public Game(int rewardSize, int profit, int cost, double mult, double lowestMult, double mostMult) {
		this(rewardSize, profit, cost, mult);
		this.lowestMult = lowestMult;
		this.mostMult = mostMult;
	}

	/**
	 * 设置奖池参数<br>
	 * 默认最少获得 cost 的10%<br>
	 * 默认最高奖励 cost 的100%<br>
	 * 
	 * @param rewardSize
	 *            奖池大小
	 * @param profit
	 *            盈利人数
	 * @param cost
	 *            参与成本(单位分)
	 * @param mult
	 *            参与比例(扣除佣金后所剩成本比例)
	 */
	public Game(int rewardSize, int profit, int cost, double mult) {
		this(rewardSize, profit, cost);
		this.mult = mult;
	}

	/**
	 * 设置奖池参数<br>
	 * 默认参与比例为60%<br>
	 * 默认最少获得 cost 的10%<br>
	 * 默认最高奖励 cost 的100%<br>
	 * 
	 * @param rewardSize
	 *            奖池大小
	 * @param profit
	 *            盈利人数
	 * @param cost
	 *            参与成本(单位分)
	 * @param mult
	 *            参与比例(扣除佣金后所剩成本比例)
	 */
	public Game(int rewardSize, int profit, int cost) {
		this(profit, cost);
		this.rewardSize = rewardSize;
	}

	/**
	 * 设置奖池参数<br>
	 * 默认奖池大小为100<br>
	 * 默认参与比例为60%<br>
	 * 默认最少获得 cost 的10%<br>
	 * 默认最高奖励 cost 的100%<br>
	 * 
	 * @param profit
	 *            盈利人数
	 * @param cost
	 *            参与成本(单位分)
	 */
	public Game(int profit, int cost) {
		this.profit = profit;
		this.cost = cost;
	}

	/**
	 * 设置奖池参数<br>
	 * 默认奖池大小为100<br>
	 * 默认参与比例为60%<br>
	 * 默认最少获得 cost 的10%<br>
	 * 默认最高奖励 cost 的100%<br>
	 * 
	 * @param cost
	 *            参与成本(单位分)
	 */
	public Game(int cost) {
		this.cost = cost;
	}

	/**
	 * 设置奖池参数<br>
	 * 默认奖池大小为100<br>
	 * 默认盈利人数为15人<br>
	 * 默认参与成本为100分<br>
	 * 默认参与比例为60%<br>
	 * 默认最少获得 cost 的10%<br>
	 * 默认最高奖励 cost 的100%
	 */
	public Game() {
	}
}
